package com.diandian.backend.order.service;

import com.diandian.backend.order.dto.OrderOfflineAddParamDTO;
import com.diandian.backend.order.dto.OrderOfflineDeleteByIdParamDTO;
import com.diandian.backend.order.dto.OrderOfflineDouyinLifeAddParamDTO;
import com.diandian.backend.order.dto.OrderOfflineDetailParamDTO;
import com.diandian.backend.order.param.OrderOfflineAddParam;
import com.diandian.backend.order.domain.OrderOffline;
import com.diandian.backend.order.mapper.OrderOfflineMapper;
import com.diandian.backend.order.resp.OrderOfflineDetailResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderOfflineService {

    @Autowired
    private OrderOfflineMapper orderOfflineMapper;

    @Autowired
    private OrderOfflineDouyinLifeService orderOfflineDouyinLifeService;

    /**
     * 接收零售系统推送的订单消息
     * @param param
     */
    @Transactional(rollbackFor = Exception.class)
    public void acceptSellOrderInfo(OrderOfflineAddParam param){

        param.check();
        String belong = param.getBelong();

        // 根据订单号查询是否已存在 todo 根据订单号加锁，避免高并发问题
        OrderOfflineDetailParamDTO detailParamDTO = new OrderOfflineDetailParamDTO();
        detailParamDTO.setOfflineOrderNum(param.getOfflineOrderNum());
        detailParamDTO.setBelong(belong);
        OrderOfflineDetailResp orderMainDetail = getDetail(detailParamDTO);

        // 删除历史数据
        if(orderMainDetail != null){
            // 删除主表数据
            OrderOfflineDeleteByIdParamDTO deleteByIdParamDTO = new OrderOfflineDeleteByIdParamDTO();
            deleteByIdParamDTO.setId(orderMainDetail.getId());
            orderOfflineMapper.deleteById(deleteByIdParamDTO);

            // 删除从表数据
            orderOfflineDouyinLifeService.deleteByOfflineOrderNum(belong, param.getOfflineOrderNum());
        }

        // 新增主表记录
        OrderOfflineAddParamDTO addParamDTO = new OrderOfflineAddParamDTO();
        addParamDTO.setOfflineOrderNum(param.getOfflineOrderNum());
        addParamDTO.setBelong(belong);
        addParamDTO.setMobile(param.getMobile());
        addParamDTO.setOrderTime(param.getOrderTime());
        addParamDTO.setOfflineStoreCode(param.getOfflineStoreCode());
        addParamDTO.setOfflineStoreName(param.getOfflineStoreName());
        addParamDTO.setPayWay(param.getPayWay());
        addParamDTO.setPayMoney(param.getPayMoney());
        add(addParamDTO);

        // 新增从表记录
        List<String> onlineOrderNum = param.getOnlineOrderNums();
        List<OrderOfflineDouyinLifeAddParamDTO> dataList = new ArrayList<>();
        for (int i = 0; i < onlineOrderNum.size(); i++) {
            OrderOfflineDouyinLifeAddParamDTO itemDTO = new OrderOfflineDouyinLifeAddParamDTO();
            itemDTO.setBelong(belong);
            itemDTO.setOfflineOrderNum(param.getOfflineOrderNum());
            itemDTO.setOnlineOrderNum(onlineOrderNum.get(i));
            dataList.add(itemDTO);
        }
        orderOfflineDouyinLifeService.batchAdd(dataList);
    }

    public OrderOfflineDetailResp getDetail(OrderOfflineDetailParamDTO param) {

        OrderOffline dbData = orderOfflineMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        OrderOfflineDetailResp result = new OrderOfflineDetailResp();
        result.setId(dbData.getId());
        result.setOfflineOrderNum(dbData.getOfflineOrderNum());
        result.setMobile(dbData.getMobile());
        result.setOrderTime(dbData.getOrderTime());
        result.setOfflineStoreCode(dbData.getOfflineStoreCode());
        result.setOfflineStoreName(dbData.getOfflineStoreName());
        result.setPayWay(dbData.getPayWay());
        result.setPayMoney(dbData.getPayMoney());
        result.setOnlineOrderNum(dbData.getOnlineOrderNum());
        result.setCreateTime(dbData.getCreateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public void add(OrderOfflineAddParamDTO param) {
        param.setCreateTime(new Date());
        param.setDel(0);
        orderOfflineMapper.add(param);
    }
}
